package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author robin
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User queryUser(Long id) {
        return userDao.select(id);
    }


    @Override
    public PageInfo queryPage(String name, Integer pageNum, Integer pageSize) {
        //1.设置分页参数：pageNum,pageSize
        PageHelper.startPage(pageNum, pageSize);
        //2.查询列表数据：select * from user where name=#{name}
        List<User> users = userDao.selectPage(name);
        //3.组装分页查询sql语句：select * from user where name=#{name} limit (pageNum-1)*pageSize,pageSize;
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public void deleteUser(Long id) {
        userDao.delete(id);
    }

    @Override
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    public void insertUser(User user) {
        userDao.insert(user);
    }

}
